package com.huawei.experiencereuse;


import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.huawei.experiencereuse.localmodel.LocalModelAgent;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ExperienceReuseAction";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        File dir = getDir("assets", Context.MODE_PRIVATE);
        File targetDir = new File(dir, "models");
        String targetDirPath = targetDir.getAbsolutePath() + File.separator;
        AssetManager assetManager = getAssets();
        LocalModelAgent.getInstance().initModel(assetManager, targetDirPath);;

        Button buttonDache = findViewById(R.id.buttonDache);
        Button buttonBookHotel = findViewById(R.id.buttonBookHotel);

        buttonDache.setOnClickListener(v -> {
            // 按钮点击时，仅触发事件（由AccessibilityService监听）
            Toast.makeText(this, "触发打车请求", Toast.LENGTH_SHORT).show();
        });
        buttonBookHotel.setOnClickListener(v -> {
            // 按钮点击时，仅触发事件（由AccessibilityService监听）
            Toast.makeText(this, "触发预定酒店请求", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void onDestroy() {
        LocalModelAgent.getInstance().deinitModel();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 检查服务是否启用，直接用Toast提示（替代TextView显示）
        if (!isAccessibilityServiceEnabled()) {
            Toast.makeText(this, "请先启用辅助功能服务", Toast.LENGTH_LONG).show();
            // 引导用户去设置
            startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
        }
    }

    /**
     * 检查辅助功能服务是否已开启
     */
    private boolean isAccessibilityServiceEnabled() {
        String serviceName = getPackageName() + "/" + ExperienceReuseService.class.getName();
        try {
            int enabled = Settings.Secure.getInt(
                    getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED
            );
            if (enabled == 1) {
                String enabledServices = Settings.Secure.getString(
                        getContentResolver(),
                        Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
                );
                return enabledServices != null && enabledServices.contains(serviceName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}